/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

// module
import Taro, { Component } from '@tarojs/taro';
import { AtInput, AtActionSheet, AtActionSheetItem, } from 'taro-ui'

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast, Loading, Modal, ActionSheet} from '../../library/func';
import Icon from '../../project/style/icon';
import Http from '../../project/func/http/http';
import '../../library/style/patch.css';

// 引用组件
// import { Addcbtn, Editcunit } from '../../project/comp';

/******************************************************************************************
 * Export Class
 ******************************************************************************************/


export default class EditIndex extends Component {
	constructor(props) {
		super(props);

		/**
		 * state
		 */
		this.state = {
			detail:{},
			// 是否显示气泡
			show: false,
			// 是否为管理员
			is_master: false
		}
	}
	
	/*------------------------------------------------------------------------------------
	* 声明周期
	*-----------------------------------------------------------------------------------*/
	// didmount
	componentDidMount(){
		this.getData()
	}

	// didshow
	componentDidShow(){
		this.getData()
	}

	getData = ()=> {

		// 检测是否为管理员
		Http.AuthOk((res)=>{
			this.setState({
				is_master:res
			})
		})

		// 取数据
		Http.Loading({
			url: 'material/detail.php?id='+ this.$router.params.id,
			success:(data)=>{
				this.setState({
					detail: data
				})
			}
		})

	}

    /*------------------------------------------------------------------------------------
	 * 自定义方法
	 *-----------------------------------------------------------------------------------*/
	changeValue = value =>{
		this.setState({
			value:value
		})
	}
	toToast(){
		Toast.text('dianji')
	}

	// 点击取消按钮
	onCancel = ()=> {
		this.setState({
			show: false
		})
	}
	// 点击遮罩层
	onClose = ()=> {
		this.setState({
			show: false
		})
	}

	//  删除照片
	deleteAlbum = ()=> {
		Modal.confirm('确定删除这张照片吗?', '删除后不可恢复', ()=>{
			Http.post({
				url:'material/logic/delete.php',
				data:{
					id: this.state.detail.id
				},
				success:(data)=>{
					Toast.success('删除成功')
					setTimeout(()=>{
						wx.navigateBack({
					        delta: 1
					     })
					},1500)
				}

			})
		},()=>{
			// 取消不进行操作
		})
	}

	// 长按图片
	catchlong = ()=> {
		this.setState({
			show:true
		})
	}

	// 保存图片
	addAblum = ()=> {
		wx.authorize({
			scope:'scope.writePhotosAlbum',
			success: (res)=> {
				wx.downloadFile({
					url: this.state.detail.set_image,
					success: (res)=> {
					  // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容 res.tempFilePath
					  if (res.statusCode === 200) {
						// 下载成功后再保存到本地
						wx.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,//返回的临时文件路径，下载后的文件会存储到一个临时文件
							success: function (res) {
							  wx.showToast({
								title: '成功保存到相册',
								icon: 'success'
							  })
							}
						  })
					  }
					},
					fail: (res)=> {
					},
					complete: (res) => {
						
					}
				})
			},
			fail: (res)=> {
				wx.openSetting({
					success (res) {
					}
				  })
			}
		})
	}
	/**
	 * 渲染
	 */
	render() {
		return (
			<CPage>
				<CContent style='width:100vw;height:100vh;margin-bottom:100rpx;margin-top:-100rpx;display:flex;' bg='#fff' x='center' y='center'>
                    <CColumn style='width:100vw;height:100%;display:flex;' x='center' y='center'>
                        <CHand style='margin-top:50rpx;'>
                            <Image style='width:100vw;' mode='widthFix' src={this.state.detail.set_image}  onLongPress={this.catchlong} />
                        </CHand>
                    </CColumn>
				</CContent>
                <CFooter h='50' bg='#cbb383' y='center' px='10'>
                    <CBetween style='flex:1' y='center' px='15'>
						<CLine x='right'>
							<CSvg w='30' h='30' icon={Icon.shareaf} color='#fff' onClick={this.addAblum}></CSvg>
						</CLine>
						<CLine x='right' pr='10'>
							{/* 为管理员时显示 */}
							{this.state.is_master == true && 
								<CSvg w='25' h='25' icon={Icon.lajit} color='#fff' onClick={this.deleteAlbum}></CSvg>
							}
						</CLine>
                    </CBetween>
                </CFooter>
				{/* 长按图片保存 */}
				{/* 点击操作显示出来的层 */}
				{/* <AtActionSheet isOpened={this.state.show} cancelText='取消' onCancel={this.onCancel} onClose={this.onClose}>
					<AtActionSheetItem>
						<CItemX x='center' y='center' onClick={this.addAblum}>
							<CText size='16' color='#2e2e2e'>保存图片</CText>
						</CItemX>
					</AtActionSheetItem>
				</AtActionSheet> */}
			</CPage>
		);
	}
}

